package com.my.yc.d0224;

import com.google.gson.Gson;
import com.my.yc.bean.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "NameItemServlet", value = "/NameItemServlet")
public class NameItemServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final String name = request.getParameter("name");
        List<String> list = new ArrayList<>();
        for (String n: NameServlet.names){
            // 判断n是否以name开头
            if (n.toLowerCase().startsWith(name.toLowerCase())){
                list.add(n);
            }
        }
        Result result = new Result(1, null, list);

        Gson gson = new Gson();
        final String json = gson.toJson(result);
        response.getWriter().printf(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
